<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Loader;
use think\Request;
use think\Session;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;

class Common extends Controller
{
    protected $adminInfo;
    // 公共
    public function _initialize()
    {
        $whoops = new Run;
        $whoops->pushHandler(new PrettyPageHandler);
        $whoops->register();

        //parent::_initialize();
        if (!Session::has('admin', 'mmjtAdmin')) {
            $this->redirect("Login/index");
        } else {
            $adminId = Session::get('admin', 'mmjtAdmin');
            $adminId = Db::name('admin')->where(['id' => $adminId['id'], 'name' => $adminId['name'], 'type' => $adminId['type'], 'hide' => $adminId['hide'], 'password' => $adminId['password']])->find();
            if (!$adminId) {
                session(null, 'mmjtAdmin');
                $this->redirect("Login/index");
            } else {
                $this->adminInfo = $adminId;
                if ($adminId['id'] != 1 && $adminId['type'] != 1) {
                    $controller = strtolower(request()->controller());
                    $action = strtolower(request()->action());
                    $if = 0;
                    if ($controller == 'index' && $action == 'index') {
                        $if = 1;
                    }
                    if ($controller == 'index' && $action == 'out') {
                        $if = 1;
                    }
                    if ($controller == 'index' && $action == 'main') {
                        $if = 1;
                    }
                    if (in_array($action, ['upload', 'uploads', 'videoUploads', 'location', 'watch'])) {
                        $if = 1;
                    }

                    $admin_cate_2 = [
                        'article&authadd',
                        'article&pingadd',
                        'article&pingdel',
                        'article&pingxaidel',
                        'member&add',
                        'member&invitecode',
                        'member&certificationedit',
                        'customerservice&topupprev',
                        'member&personal',
                        'member&del',
                        'usertalk&shenhewords'
                        //'member&certificationdel',
                    ];

                    if ($if == 0) {
                        $adminTypeData = Db::name('admin_type')->where('id', $adminId['type'])->value('data');
                        $adminCategoryId = Db::name('category')->where(['controller' => $controller, 'function' => $action])->value('id');
                        $permissionSucceeded = 0;
                        if ($adminCategoryId && $adminTypeData) {
                            $a = json_decode($adminTypeData, true);

                            foreach ($a as $v) {
                                if ($adminCategoryId == $v) {
                                    $permissionSucceeded = 1;
                                    break;
                                }
                            }
                        } else {
                            //$this->error('菜单不存在');

                            if (in_array("$controller&$action", $admin_cate_2)) {
                                $permissionSucceeded = 1;
                            } else {
                                exit(json_encode(['err' => '菜单不存在'], JSON_UNESCAPED_UNICODE));
                            }
                        }
                        // 开始判断权限  0 没权限  1有权限
                        if ($permissionSucceeded == 0) {
                            //echo "无权限";
                            exit(json_encode(['err' => '无权限'], JSON_UNESCAPED_UNICODE));
                        }
                    }
                }

                if (request()->isAjax()) {
                    $request = Request::instance();
                    $dispatch = $request->dispatch();
                    $controller = $dispatch['module'][1];
                    $action = $dispatch['module'][2];

                    $validate = Loader::getModuleAndClass($controller, 'validate', false);

                    if (class_exists($validate[1])) {
                        $validate = Loader::validate($controller);

                        if ($scene = $validate->hasScene($action)) {
                            $params = input('post.');

                            if (!$validate->scene($scene)->check($params)) {
                                exit(json_encode(['err' => $validate->getError()]));
                            }
                        }
                    }
                }
            }
        }
    }
}
